package adb.group.entity;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Register
 */
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String street=request.getParameter("street");
		String city=request.getParameter("city");
		String state=request.getParameter("state");
		String zip=request.getParameter("zip");
		String homephone=request.getParameter("homephone");
		String workphone=request.getParameter("workphone");
		
		String email=request.getParameter("email");
		String marriage=request.getParameter("marriage");
		String company=request.getParameter("company");
		String policy=request.getParameter("policy");
		String group=request.getParameter("group");
		String companyphone=request.getParameter("companyphone");
		String sfname=request.getParameter("sfname");
		String slname=request.getParameter("slname");
		String saddress=request.getParameter("saddress");
		String relation=request.getParameter("relation");
		String sphone=request.getParameter("sphone");
		String submit=request.getParameter("Submit");
		
		
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection connection = (Connection) DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:ORCL", "dbfp_dba",
					"dbfpdba");
			
			CallableStatement proc = null;
			if(submit.equalsIgnoreCase("Submit")){
				Date birthday = null;
				try {
					birthday = new SimpleDateFormat("MM/DD/YYYY",Locale.ENGLISH).parse(request.getParameter("birthday"));
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				String fname=request.getParameter("fname");
				String lname=request.getParameter("lname");
			proc = connection.prepareCall("{ call register(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
		      proc.setString(1,fname );
		      proc.setString(2,lname );
		      proc.setString(3, street);
		      proc.setString(4, city);
		      proc.setString(5, state);
		      proc.setString(6, zip);
		      proc.setString(7, homephone);
		      proc.setString(8, workphone);
		      proc.setDate(9,new java.sql.Date(birthday.getTime()));
		      proc.setString(10, email);
		      proc.setString(11, marriage);
		      proc.setString(12, company);
		      proc.setLong(13, Long.parseLong(policy));
		      proc.setLong(14, Long.parseLong(group));
		      proc.setLong(15, Long.parseLong(companyphone));
		      proc.setString(16, sfname);
		      proc.setString(17, slname);
		      proc.setString(18, saddress);
		      proc.setString(19, relation);
		      proc.setString(20, sphone);
		      proc.execute();
		      PreparedStatement getuserID=connection.prepareStatement("select * from user_t where user_id = (select max(user_id) from user_t)");
		      ResultSet rSet=getuserID.executeQuery();
		      rSet.next();
		      String userId=rSet.getString(1);
		      String password=rSet.getString(2);
		      request.getSession().setAttribute("userid", userId);
		      request.getSession().setAttribute("password", password);
		      PreparedStatement getpatientID=connection.prepareStatement("select max(patient_id) from patient_t");
		      ResultSet set=getpatientID.executeQuery();
		      set.next();
		      String patient_id=set.getString(1);
		      request.getSession().setAttribute("pid",patient_id);
		      response.sendRedirect("/project/resources/common/header.jsp");
			}else{
				int personid=Integer.parseInt(request.getSession().getAttribute("personID").toString());
				proc = connection.prepareCall("{ call updatePatient(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
			      proc.setInt(1,personid);
			      proc.setString(2, street);
			      proc.setString(3, city);
			      proc.setString(4, state);
			      proc.setString(5, zip);
			      proc.setString(6, homephone);
			      proc.setString(7, workphone);
			      proc.setString(8, email);
			      proc.setString(9, marriage);
			      proc.setString(10, company);
			      proc.setLong(11, Long.parseLong(policy));
			      proc.setLong(12, Long.parseLong(group));
			      proc.setLong(13, Long.parseLong(companyphone));
			      proc.setString(14, sfname);
			      proc.setString(15, slname);
			      proc.setString(16, saddress);
			      proc.setString(17, relation);
			      proc.setString(18, sphone);
			      proc.execute();
			      response.sendRedirect("/project/resources/patient/patientmain.jsp");
				
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

}
